// component/exam_handle/exam_handle.js
var app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    subjectType:{
      type:String
    },
    current:{
      type:Number
    },
    subjectList:{
      type:Array
    },
    examSetting:{
      type:Object
    },
    total:{
      type:Number
    },
    subjectList:{
      type:Array
    },
    remainTime:{
      type:String
    },
    btnTitle:{
      type:String,
      value:'下一题'
    }



  },

  /**
   * 组件的初始数据
   */
  data: {

  },
 

  /**
   * 组件的方法列表
   */
  methods: {

    lastSubject: function () {

      if (this.data.current == this.data.subjectList.length - 1) {
        this.setData({ btnTitle: '下一题' });
      }

      if (this.data.current > 0) {
        this.triggerEvent('updateCurrent', --this.data.current);
      }
    },
    nextSubject: function () {
      
      if (this.data.current < this.data.subjectList.length - 1) {
        this.triggerEvent('updateCurrent', ++this.data.current);
      }else{
        this.submitExam();
      }

      if (this.data.current == this.data.subjectList.length - 1) {
        this.setData({ btnTitle: '交卷' });
      }


    },
   
    //交卷
    submitExam:function(){
      var me = this;
      wx.showModal({
        title: '',
        content: '确认交卷',
        showCancle:true,
        success:function(res){
          if (res.confirm){
            var subjectList = me.data.subjectList;
            var setting = me.data.examSetting;
            var title = '正在评分';
            if(me.data.subjectType == 'com'){
              var totalScore = -1;
              title="正在交卷";
            }else{
              var totalScore = 0;
              for (var i = 0; i < subjectList.length; i++) {
                if (me.data.subjectType == 'single') {
                  if (subjectList[i].selectOption == subjectList[i].anwser) {
                    totalScore += parseInt(setting.perScore);
                  }
                } else if (me.data.subjectType == 'mutilple') {
                  if (typeof (subjectList[i].selectOption) != 'undefined') {
                    if (subjectList[i].selectOption.sort().join() == subjectList[i].anwser) {
                      totalScore += parseInt(setting.perScore);
                    }
                  }

                } else if (me.data.subjectType == 'judge') {
                  if (typeof (subjectList[i].result) != 'undefined') {
                    if (subjectList[i].result == subjectList[i].answer) {
                      totalScore += parseInt(setting.perScore);
                    }
                  }
                }
              }

            }

            wx.showLoading({
              title: title,
            })

            
             
            

            app.util.request({
              url: "entry/wxapp/saveExamResult",
              data: {
                m: "rs_anwser",
                subjectType: me.data.subjectType,
                totalScore: totalScore,
                examSetting: JSON.stringify(setting),
                subjects: JSON.stringify(subjectList),
                userId: app.getUserId()
              },
              success: function (data) {
                console.log(data);
              }
            })
            me.triggerEvent('updateTotalScore', totalScore);
            wx.hideLoading();
          }
        }
      })
    }
  }
})
